package com.zyh.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyh.server.entity.Order;
import com.zyh.server.entity.OrderDetail;
import com.zyh.server.entity.User;
import com.zyh.server.mapper.OrderDetailMapper;
import com.zyh.server.mapper.UserMapper;
import com.zyh.server.service.OrderService;
import com.zyh.server.mapper.OrderMapper;
import com.zyh.server.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author ZYH
 * @description 针对表【t_order】的数据库操作Service实现
 * @createDate 2024-11-19 09:17:09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
		implements OrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private OrderDetailMapper orderDetailMapper;
	// @Autowired
	// private UserMapper userMapper;

	/**
	 * 根据订单编号查询订单信息
	 *
	 * @param orderId
	 *
	 * @return
	 */
	@Override
	public Order getOrderById(String orderId) {
		Order order = orderMapper.selectById(orderId);
		if (Objects.nonNull(order)) {
			// 获取订单详情集合
			List<OrderDetail> list = orderDetailMapper.selectByOrderId(orderId);
			order.setOrderDetailList(list);
			// 根据订单号查询用户id
			// User user = userMapper.selectById(order.getUserId());
			// order.setUser(user);
		}
		return order;
	}

	@Override
	public String checkOrder(Integer userId, Integer miaoshaGoodsId) {
		return orderMapper.checkOrder(userId, miaoshaGoodsId);
	}

	@Override
	public List<Order> findOrderListByUserId(Integer userId) {
		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId);
		List<Order> orderList = orderMapper.selectList(wrapper);
		if (Objects.nonNull(orderList)) {
			for (Order order : orderList) {
				List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrderId(order.getId());
				order.setOrderDetailList(orderDetailList);
			}
		}
		return orderList;
	}
}




